/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package BTL;

import java.io.*;
import javax.swing.JOptionPane;
import static javax.swing.JOptionPane.*;
import java.util.*;
import javax.swing.JComboBox;
/**
 *
 * @author Administrator
 */
public class AddRule extends javax.swing.JFrame {

    /**
     * Creates new form AddRule
     */
    MainGUI mainGUI;
    public AddRule(MainGUI parent) {
        initComponents();
        mainGUI = parent;
        
        LoadEvents(cbCondition1, mainGUI.listEvent);
        LoadEvents(cbCondition2, mainGUI.listEvent);
        LoadEvents(cbResult, mainGUI.listEvent);
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cbTypeRule = new javax.swing.JComboBox();
        cbCondition1 = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        cbCondition2 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        cbResult = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        spCF = new javax.swing.JSpinner();
        lbTypeRule = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        btAddRule = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setTitle("Điều chỉnh luật");
        setResizable(false);

        cbTypeRule.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Luật đơn", "Luật And", "Luật Or" }));
        cbTypeRule.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbTypeRuleItemStateChanged(evt);
            }
        });
        cbTypeRule.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cbTypeRuleMouseClicked(evt);
            }
        });
        cbTypeRule.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbTypeRuleActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Chọn loại luật");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setText("Điều kiện");

        cbCondition2.setEnabled(false);
        cbCondition2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbCondition2ActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("Điều kiện thứ hai");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setText("Kết luận");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel5.setText("Độ tin cậy");

        spCF.setModel(new javax.swing.SpinnerNumberModel(Float.valueOf(0.5f), Float.valueOf(-1.0f), Float.valueOf(1.0f), Float.valueOf(0.01f)));

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel7.setText("IF");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel6.setText("THEN");

        btAddRule.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btAddRule.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/them luat.png"))); // NOI18N
        btAddRule.setText("Áp dụng");
        btAddRule.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAddRuleActionPerformed(evt);
            }
        });

        jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/them.png"))); // NOI18N

        jButton1.setText("Thoát");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(48, 48, 48)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(cbCondition1, javax.swing.GroupLayout.PREFERRED_SIZE, 333, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cbTypeRule, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(43, 43, 43)
                        .addComponent(jLabel8))
                    .addComponent(jLabel2)
                    .addComponent(cbCondition2, javax.swing.GroupLayout.PREFERRED_SIZE, 333, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(216, 216, 216)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(42, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(spCF, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel5))
                                .addGap(121, 121, 121)
                                .addComponent(btAddRule))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(274, 274, 274))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addGap(39, 39, 39)
                                .addComponent(cbResult, javax.swing.GroupLayout.PREFERRED_SIZE, 340, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(49, 49, 49))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(lbTypeRule, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(378, 378, 378))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(68, 68, 68)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cbTypeRule, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jLabel8)))
                .addGap(13, 13, 13)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(cbCondition1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cbCondition2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lbTypeRule, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(40, 40, 40)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbResult, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))
                        .addGap(46, 46, 46)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(spCF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btAddRule, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(20, 20, 20))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void LoadEvents(JComboBox cbBox, ArrayList<Event> listEvent)
    {        
        int i;
        String sEvent;
        cbBox.removeAllItems();
        for (i = 0; i < listEvent.size(); i++)
        {
            sEvent = listEvent.get(i).nID + "|" + listEvent.get(i).sDetail;
            cbBox.addItem(sEvent);
        }
    }
    private void cbTypeRuleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbTypeRuleActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbTypeRuleActionPerformed

    private void cbTypeRuleMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cbTypeRuleMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_cbTypeRuleMouseClicked

    private void cbTypeRuleItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbTypeRuleItemStateChanged
        // TODO add your handling code here:
        String sRule = (String)cbTypeRule.getSelectedItem();
        if (sRule.equals("Luật đơn"))
        {
            lbTypeRule.setText("");
            cbCondition2.setEnabled(false);
        }
        if (sRule.equals("Luật And"))
        {
            lbTypeRule.setText("AND");
            cbCondition2.setEnabled(true);
        }
        if (sRule.equals("Luật Or"))
        {
            lbTypeRule.setText("OR");
            cbCondition2.setEnabled(true);
        }
    }//GEN-LAST:event_cbTypeRuleItemStateChanged
    
    private boolean CheckSingleRuleExist(SingleRule ruleSingle)
    {
        int nIndex;
        boolean bExist;
        String stream;
        FileOutputStream fileOut;
        PrintWriter printFile;
        SingleRule ruleTemp;
        
        bExist = false;
        for (nIndex = 0; nIndex < mainGUI.listSingleRule.size(); nIndex++)
        {
            ruleTemp = mainGUI.listSingleRule.get(nIndex);
            if ((ruleTemp.nIDCondition == ruleSingle.nIDCondition)
                && (ruleTemp.nIDResult == ruleSingle.nIDResult))
            {
                bExist = true;
                break;
            }    
        }
        if (!bExist)
            return false;
        if (JOptionPane.showConfirmDialog(this, "Luật đã có trong cơ sở tri thức. Bạn có muốn cập nhật lại độ tin cậy không?", 
                "Xác nhận", YES_NO_OPTION) == NO_OPTION)
            return true;
        
        ruleTemp = mainGUI.listSingleRule.set(nIndex, ruleSingle);
                
        try
        {
            fileOut = new FileOutputStream("src\\KB\\SingleRule.txt", false);//false để xóa nội dung file trước khi ghi
        }
        catch(Exception e)
        {
            mainGUI.listSingleRule.set(nIndex, ruleTemp);//Khôi phục lại luật cũ
            JOptionPane.showMessageDialog(null, "Không tìm thấy tệp luật đơn", "Lỗi ghi luật vào cơ sở tri thức", ERROR_MESSAGE);
            return true;
        }
        
        printFile = new PrintWriter(fileOut);
        
        stream = "";
        for (nIndex = 0; nIndex < mainGUI.listSingleRule.size(); nIndex++)
        {
            ruleTemp = mainGUI.listSingleRule.get(nIndex);            
            stream = stream + 
                    "\r\n" + ruleTemp.nIDCondition 
                    + "|" + ruleTemp.nIDResult 
                    + "|" + ruleTemp.CF 
                    + "\r\n";
        }
        printFile.write(stream);
        printFile.close();
        mainGUI.formViewRule.ShowSingleRuleList();
        
        JOptionPane.showMessageDialog(this, "Sửa thành công luật", "Thông báo", INFORMATION_MESSAGE);
        
        return true;
    }
    
    private boolean AddSingleRule(int nIDCondition, int nIDResult, float dCF)
    {
        String stream;
        SingleRule ruleSingle;
        FileOutputStream fileOut;
        PrintWriter printFile;
        
        ruleSingle = new SingleRule(nIDCondition, nIDResult, dCF);
        if (CheckSingleRuleExist(ruleSingle))
            return true;
        if (!mainGUI.listSingleRule.add(ruleSingle))
        {
            JOptionPane.showMessageDialog(this, "Lỗi khi thêm luật vào bộ nhớ", "Lỗi", INFORMATION_MESSAGE);            
            return false;
        }
        
        try
        {
            fileOut = new FileOutputStream("src\\KB\\SingleRule.txt", true);//true để ghi tiếp vào file 
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, "Không tìm thấy tệp luật đơn", "Lỗi ghi luật vào cơ sở tri thức", ERROR_MESSAGE);
            return false;
        }
        
        printFile = new PrintWriter(fileOut);
        
        stream = "\r\n" 
                + ruleSingle.nIDCondition + "|" 
                + ruleSingle.nIDResult + "|" 
                + ruleSingle.CF
                + "\r\n";
        printFile.write(stream);
        printFile.close();
        mainGUI.formViewRule.ShowSingleRuleList();
                
        JOptionPane.showMessageDialog(this, "Thêm thành công luật", "Thông báo", INFORMATION_MESSAGE);
        return true;
    }
    
    private boolean CheckAndRuleExist(AndRule ruleAnd)
    {
        int nIndex;
        boolean bExist;
        String stream;
        FileOutputStream fileOut;
        PrintWriter printFile;
        AndRule ruleTemp;
        
        bExist = false;
        for (nIndex = 0; nIndex < mainGUI.listAndRule.size(); nIndex++)
        {
            ruleTemp = mainGUI.listAndRule.get(nIndex);
            if ((ruleTemp.nIDResult == ruleAnd.nIDResult)
                && (((ruleTemp.nIDCondition1 == ruleAnd.nIDCondition1) 
                        && (ruleTemp.nIDCondition2 == ruleAnd.nIDCondition2))
                    || ((ruleTemp.nIDCondition1 == ruleAnd.nIDCondition2) 
                        && (ruleTemp.nIDCondition2 == ruleAnd.nIDCondition1))))
            {
                bExist = true;
                break;
            }    
        }
        if (!bExist)
            return false;
        if (JOptionPane.showConfirmDialog(this, "Luật đã có trong cơ sở tri thức. Bạn có muốn cập nhật lại độ tin cậy không?", 
                "Xác nhận", YES_NO_OPTION) == NO_OPTION)
            return true;
        
        ruleTemp = mainGUI.listAndRule.set(nIndex, ruleAnd);
                
        try
        {
            fileOut = new FileOutputStream("src\\KB\\AndRule.txt", false);//false để xóa nội dung file trước khi ghi
        }
        catch(Exception e)
        {
            mainGUI.listAndRule.set(nIndex, ruleTemp);//Khôi phục lại luật cũ
            JOptionPane.showMessageDialog(null, "Không tìm thấy tệp luật And", "Lỗi ghi luật vào cơ sở tri thức", ERROR_MESSAGE);
            return true;
        }
        
        printFile = new PrintWriter(fileOut);
        
        stream = "";
        for (nIndex = 0; nIndex < mainGUI.listAndRule.size(); nIndex++)
        {
            ruleTemp = mainGUI.listAndRule.get(nIndex);            
            stream = stream + 
                    "\r\n" + ruleTemp.nIDCondition1
                    + "|" + ruleTemp.nIDCondition2
                    + "|" + ruleTemp.nIDResult 
                    + "|" + ruleTemp.CF 
                    + "\r\n";
        }
        printFile.write(stream);
        printFile.close();
        mainGUI.formViewRule.ShowAndRuleList();
        
        JOptionPane.showMessageDialog(this, "Sửa thành công luật", "Thông báo", INFORMATION_MESSAGE);
        return true;
    }
    private boolean AddAndRule(int nIDCondition1, int nIDCondition2, int nIDResult, float dCF)
    {
        String stream;
        AndRule ruleAnd;
        FileOutputStream fileOut;
        PrintWriter printFile;
        
        ruleAnd = new AndRule(nIDCondition1, nIDCondition2, nIDResult, dCF);
        if (CheckAndRuleExist(ruleAnd))
            return true;
        if (!mainGUI.listAndRule.add(ruleAnd))
        {
            JOptionPane.showMessageDialog(this, "Lỗi khi thêm luật vào bộ nhớ", "Lỗi", INFORMATION_MESSAGE);            
            return false;
        }
        
        try
        {
            fileOut = new FileOutputStream("src\\KB\\AndRule.txt", true);//true để ghi tiếp vào file 
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, "Không tìm thấy tệp luật And", "Lỗi ghi luật vào cơ sở tri thức", ERROR_MESSAGE);
            return false;
        }
        
        printFile = new PrintWriter(fileOut);
        
        stream = "\r\n" 
                + ruleAnd.nIDCondition1 + "|" 
                + ruleAnd.nIDCondition2 + "|" 
                + ruleAnd.nIDResult + "|" 
                + ruleAnd.CF
                + "\r\n";
        printFile.write(stream);
        printFile.close();
        mainGUI.formViewRule.ShowAndRuleList();
        
        JOptionPane.showMessageDialog(this, "Thêm thành công luật", "Thông báo", INFORMATION_MESSAGE);
        return true;
    }
    
    private boolean CheckOrRuleExist(OrRule ruleOr)
    {
        int nIndex;
        boolean bExist;
        String stream;
        FileOutputStream fileOut;
        PrintWriter printFile;
        OrRule ruleTemp;
        
        bExist = false;
        for (nIndex = 0; nIndex < mainGUI.listOrRule.size(); nIndex++)
        {
            ruleTemp = mainGUI.listOrRule.get(nIndex);
            if ((ruleTemp.nIDResult == ruleOr.nIDResult)
                && (((ruleTemp.nIDCondition1 == ruleOr.nIDCondition1) 
                        && (ruleTemp.nIDCondition2 == ruleOr.nIDCondition2))
                    || ((ruleTemp.nIDCondition1 == ruleOr.nIDCondition2) 
                        && (ruleTemp.nIDCondition2 == ruleOr.nIDCondition1))))
            {
                bExist = true;
                break;
            }    
        }
        if (!bExist)
            return false;
        if (JOptionPane.showConfirmDialog(this, "Luật đã có trong cơ sở tri thức. Bạn có muốn cập nhật lại độ tin cậy không?", 
                "Xác nhận", YES_NO_OPTION) == NO_OPTION)
            return true;
        
        ruleTemp = mainGUI.listOrRule.set(nIndex, ruleOr);
                
        try
        {
            fileOut = new FileOutputStream("src\\KB\\OrRule.txt", false);//false để xóa nội dung file trước khi ghi
        }
        catch(Exception e)
        {
            mainGUI.listOrRule.set(nIndex, ruleTemp);//Khôi phục lại luật cũ
            JOptionPane.showMessageDialog(null, "Không tìm thấy tệp luật Or", "Lỗi ghi luật vào cơ sở tri thức", ERROR_MESSAGE);
            return true;
        }
        
        printFile = new PrintWriter(fileOut);
        
        stream = "";
        for (nIndex = 0; nIndex < mainGUI.listOrRule.size(); nIndex++)
        {
            ruleTemp = mainGUI.listOrRule.get(nIndex);            
            stream = stream + 
                    "\r\n" + ruleTemp.nIDCondition1
                    + "|" + ruleTemp.nIDCondition2
                    + "|" + ruleTemp.nIDResult 
                    + "|" + ruleTemp.CF 
                    + "\r\n";
        }
        printFile.write(stream);
        printFile.close();
        mainGUI.formViewRule.ShowOrRuleList();
        
        JOptionPane.showMessageDialog(this, "Sửa thành công luật", "Thông báo", INFORMATION_MESSAGE);
        return true;
    }
    private boolean AddOrRule(int nIDCondition1, int nIDCondition2, int nIDResult, float dCF)
    {
        String stream;
        OrRule ruleOr;
        FileOutputStream fileOut;
        PrintWriter printFile;
        
        ruleOr = new OrRule(nIDCondition1, nIDCondition2, nIDResult, dCF);
        if (CheckOrRuleExist(ruleOr))
            return true;
        if (!mainGUI.listOrRule.add(ruleOr))
        {
            JOptionPane.showMessageDialog(this, "Lỗi khi thêm luật vào bộ nhớ", "Lỗi", INFORMATION_MESSAGE);            
            return false;
        }
        
        try
        {
            fileOut = new FileOutputStream("src\\KB\\OrRule.txt", true);//true để ghi tiếp vào file 
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, "Không tìm thấy tệp luật Or", "Lỗi ghi luật vào cơ sở tri thức", ERROR_MESSAGE);
            return false;
        }
        
        printFile = new PrintWriter(fileOut);
        
        stream = "\r\n" 
                + ruleOr.nIDCondition1 + "|" 
                + ruleOr.nIDCondition2 + "|" 
                + ruleOr.nIDResult + "|" 
                + ruleOr.CF
                + "\r\n";
        printFile.write(stream);
        printFile.close();
        mainGUI.formViewRule.ShowOrRuleList();
        
        JOptionPane.showMessageDialog(this, "Thêm thành công luật", "Thông báo", INFORMATION_MESSAGE);
        return true;
    }
    private void btAddRuleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddRuleActionPerformed
        // TODO add your handling code here:
        int nIDCondition1, nIDCondition2, nIDResult;
        String s, sRule;
        int nIndex;
        float dCF;
        
        s = (String)cbCondition1.getSelectedItem();
        if ((s == null) || (s.isEmpty()))
        {
            JOptionPane.showMessageDialog(this, "Bạn chưa chọn điều kiện", "Hướng dẫn", INFORMATION_MESSAGE);
            return;
        }
        nIDCondition1 = Integer.parseInt(s.substring(0, s.indexOf('|')));
        
        sRule = (String)cbTypeRule.getSelectedItem();
        if (sRule.equals("Luật đơn"))
        {
            nIDCondition2 = -1;
        }
        else
        {
            s = (String)cbCondition2.getSelectedItem();
            if ((s == null) || (s.isEmpty()))
            {
                JOptionPane.showMessageDialog(this, "Bạn chưa chọn dủ điều kiện", "Hướng dẫn", INFORMATION_MESSAGE);
                return;
            }
            nIDCondition2 = Integer.parseInt(s.substring(0, s.indexOf('|')));
        }
        
        s = (String)cbResult.getSelectedItem();
        if ((s == null) || (s.isEmpty()))
        {
            JOptionPane.showMessageDialog(this, "Bạn chưa chọn kết luận", "Hướng dẫn", INFORMATION_MESSAGE);
            return;
        }
        nIDResult = Integer.parseInt(s.substring(0, s.indexOf('|')));      
        
        dCF = (float) spCF.getValue();
        if ((dCF < -1f) || (dCF > 1f))
        {
            JOptionPane.showMessageDialog(this, "Độ tin cậy chưa nằm trong khoảng [-1; 1]", "Hướng dẫn", INFORMATION_MESSAGE);
            return;
        }
        if (sRule.equals("Luật đơn"))
        {
           AddSingleRule(nIDCondition1, nIDResult, dCF);
        }
        if (sRule.equals("Luật And"))
        {
            AddAndRule(nIDCondition1, nIDCondition2, nIDResult, dCF);
        }
        if (sRule.equals("Luật Or"))
        {
            AddOrRule(nIDCondition1, nIDCondition2, nIDResult, dCF);                       
        }
    }//GEN-LAST:event_btAddRuleActionPerformed

    private void cbCondition2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbCondition2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbCondition2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AddRule.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AddRule.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AddRule.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AddRule.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AddRule(new MainGUI()).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAddRule;
    private javax.swing.JComboBox cbCondition1;
    private javax.swing.JComboBox cbCondition2;
    private javax.swing.JComboBox cbResult;
    private javax.swing.JComboBox cbTypeRule;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel lbTypeRule;
    private javax.swing.JSpinner spCF;
    // End of variables declaration//GEN-END:variables
}
